<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 顶部的输入框和添加商品的按钮 -->
      <el-row :gutter="20">
        <!-- 第一列为文本输入框 -->
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            @clear="searchAbility"
          >
            <el-button slot="append" icon="el-icon-search" @click="searchAbility"></el-button>
          </el-input>
        </el-col>
        <!-- 第二列为添加商品的按钮 -->
        <el-col :span="4">
          <el-button type="primary" @click="addGoodsList">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表的表格区域 -->
      <el-table :data="goodList" stripe border style="width: 100%">
        <!-- 添加索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="95px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140px">
          <!-- 这里需要作用域的插槽 -->
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGoodsList(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 商品列表的分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagesize"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagenum"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 获取商品列表请求的参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 20,
      },
      //商品列表表格的数据源
      goodList: [],
      //   商品列表的总条数
      total: 0,
    };
  },
  created: function () {
    this.getGoodsList();
  },
  methods: {
    //   定义获取商品列表的相关处理函数
    getGoodsList: async function () {
      // 调用接口
      const { data: res } = await this.$http.get(`goods`, {
        params: this.queryInfo,
      });

      if (res.meta.status !== 200) {
        return this.$message.error("获取商品列表失败!");
      }

      this.$message.success(res.meta.msg);
      //   完成赋值操作
      this.goodList = res.data.goods;
      this.total = res.data.total;
    },
    // 监听分页pagesize变化的相关处理函数
    handleSizeChange: function (pagesize) {
      this.queryInfo.pagesize = pagesize;
      this.getGoodsList();
    },
    // 监听分页pagenum变化的相关处理函数
    handleCurrentChange: function (pagenum) {
      this.queryInfo.pagenum = pagenum;
      this.getGoodsList();
    },
    // 完成商品列表的搜索功能
    searchAbility: function () {
      this.getGoodsList();
    },
    // 删除商品的相关处理函数
    delGoodsList: async function (id) {
      // 弹出框
      const result = await this.$prompt(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          //   捕获点击取消时的错误
        }
      ).catch(function (error) {
        return error;
      });
      //   如果取消删除
      if (result === "cancel") {
        return this.$message({
          type: "info",
          message: "取消删除!",
        });
      }
      //   如果点击的是确定,则调接口
      if (result === "confirm") {
        const { data: res } = await this.$http.delete(`goods/${id}`);

        if (res.meta.status !== 200) {
          return this.$message.error("删除失败!");
        }

        this.$message.success(res.meta.msg);
        // 删除之后,在重新获取商品列表的函数
        this.getGoodsList();
      }
    },
    //添加商品的相关处理函数
    addGoodsList: function () {
      // 点击添加后,跳转到添加商品的页面
      console.log(this.$router.push("/goods/add"));
    },
  },
};
</script>
<style lang="less" scoped>
</style>